package com.buss.customer.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.dic.RecordStatus;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.customer.dao.CustomerDao;
import com.buss.customer.model.Customer;
import com.buss.customer.model.CustomerClass;
import com.buss.customer.service.CustomerClassService;
import com.buss.customer.service.CustomerService;

@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerDao customerDao;
	
	@Autowired
	private CustomerClassService classService;
	
	@Override
	public Customer saveCustomer(Customer customer) {
		return customerDao.saveCustomer(customer);
	}

	@Override
	public void editCustomer(Customer customer) {
		customerDao.editCustomer(customer);
	}

	@Override
	public void removeCustomer(Long[] ids) {
		customerDao.removeCustomer(ids);
	}

	@Override
	public Customer loadCustomer(Long id) {		
		return customerDao.loadCustomer(id);
	}

	@Override
	public ResultList<Customer> queryCustomer(String companyId, Long classId,
			String name, String mnemonic, Long salesId, Boolean isCustomer,
			Boolean isSupplier, RecordStatus status, Paginalbe page,
			OrderBy orderBy) {
		List<CustomerClass> classes = classService.queryByParentIdToAll(companyId, classId);
		Long[] classIds = new Long[classes.size() + 1];
		int cnt = classes.size();
		classIds[0] = classId;
		for(int i = 0;i < cnt;i++){
			classIds[i + 1] = classes.get(i).getId();
		}
		if(orderBy == null){
			orderBy = new OrderBy();
		}
		return customerDao.queryCustomer(companyId, classIds, name, mnemonic, salesId, isCustomer, isSupplier, status, page, orderBy);
	}

}
